package com.realtyManage.springboot.Controller;

import com.alibaba.fastjson.JSON;
import com.aliyun.oss.model.LiveChannelListing;
import com.realtyManage.springboot.Model.Do.*;
import com.realtyManage.springboot.Model.Vo.BaseEntity;
import com.realtyManage.springboot.Model.Vo.RegEntity;
import com.realtyManage.springboot.Service.IUserService;
import com.realtyManage.springboot.Service.IUserTaskService;
import com.realtyManage.springboot.Service.impl.ConferenceContentServiceImpl;
import com.realtyManage.springboot.Service.impl.CouponServiceImpl;
import com.realtyManage.springboot.Utils.CodeUtils;
import com.realtyManage.springboot.Utils.SysExecRsUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Spliterator;

@Controller
@RequestMapping("/")
public class ConferenceContentController {

    @Autowired
    private ConferenceContentServiceImpl conferenceContentServiceImp;

    @Autowired
    private CouponServiceImpl couponServiceImp;

    @Autowired
    private IUserService userService;


    /**
     * 增加会议纪要
     * @param conferenceContent
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addConferenceContent",method = RequestMethod.POST)
    private String addConferenceContent(ConferenceContent conferenceContent,HttpServletRequest request){
        Integer code = -1;
         /*if (conferenceContent.getId() == null){    //前端莫名奇妙传这玩意
             conferenceContent.setId(null);
         }*/
         conferenceContent.setUserId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId().toString());
         if (conferenceContentServiceImp.addConferenceContent(conferenceContent).getId() != null){
            code = 1;
         }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /**
     * 获取我的会议纪要
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/myConferenceContent",method = RequestMethod.GET)
    private String myConferenceContent(HttpServletRequest request) {
        String time = request.getParameter("time");
        List<ConferenceContent> lst = conferenceContentServiceImp.myConferenceContent(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId(),time);
        return JSON.toJSONString(lst);
    }


    /**
     * 获取会议详情
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/myConferenceContentById",method = RequestMethod.GET)
    private String myConferenceContentById(HttpServletRequest request) {
        String id = request.getParameter("id");
        List<ConferenceContent> lst = conferenceContentServiceImp.myConferenceContentById(id);
        return JSON.toJSONString(lst);
    }


    /**
     * 删除会议纪要
     * @param conferenceContentId
     * @param request
     * @return
     */
    @RequestMapping(value = "/deleteConferenceContent",method = RequestMethod.POST)
    @ResponseBody
    private String deleteConferenceContent(String conferenceContentId, HttpServletRequest request) {
        Integer code = -1;
        List ids = new ArrayList();
        String dynamicId[] = conferenceContentId.split(",");
        for (String id : dynamicId){
            ids.add(id);
        }
        try{
            if (ids.size() > 0){
                if (conferenceContentServiceImp.deleteConferenceContent(ids) > 0){
                    code = 1;
                }else{
                    code = -1;
                }
            }
        }catch (Exception e){
            e.getMessage();
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }

    /**
     * 获取会议纪要信息
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getConferenceContent",method = RequestMethod.GET)
    private String getConferenceContent(HttpServletRequest request) {
        String content = request.getParameter("content");
        List<ConferenceContent> lst = conferenceContentServiceImp.getConferenceContent(content);
        return JSON.toJSONString(lst);
    }


    /**
     * 生成优惠劵
     * @param coupon
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/addCoupon",method = RequestMethod.POST)
    private String addCoupon(Coupon coupon,HttpServletRequest request){
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Integer code = -1;
        coupon.setUserId(userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId().toString());
        //新增优惠劵数量
        for (int i = 0;i<coupon.getNum();i++){
            Coupon c = new Coupon();
            c.setCode(CodeUtils.getCode()); //生成编码
            c.setInfoTime(sdf.format(new Date()));
            c.setImg(coupon.getImg());
            c.setUserId(coupon.getUserId());
            c.setCouponValue(coupon.getCouponValue());
            c.setEndTime(coupon.getEndTime());
            c.setCouponDetail(coupon.getCouponDetail());
            if (couponServiceImp.addCoupon(c) > 0){
                code = 1;
            }
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


    /**
     * 生成者分配优惠劵
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/distributeCoupon",method = RequestMethod.POST)
    private String distributeCoupon(HttpServletRequest request){
        String couponId = request.getParameter("couponId");
        String[] couponIds = couponId.split(",");
        String saleId = request.getParameter("saleId");
        Integer code = -1;
        for (String id : couponIds) {
            if (couponServiceImp.distributeCoupon(saleId, Integer.valueOf(id)) > 0){
                code = 1;
            }
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }

    /**
     * 生成者获取生成优惠劵没有分配列表
     * @param request
     * @return
    **/
    @ResponseBody
    @RequestMapping(value = "/findCoupon",method = RequestMethod.GET)
    private String findCoupon(HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(new Date());
        String userId = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId().toString();
        List<Coupon> lst = couponServiceImp.findCoupon(userId,time);
        return JSON.toJSONString(lst);
    }

    /**
     * 销售员获取优惠劵列表
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findSaleCoupon",method = RequestMethod.GET)
    private String findSaleCoupon(HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(new Date());
        String userId = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getId().toString();
        List<Coupon> lst = couponServiceImp.findSaleCoupon(userId,time);
        return JSON.toJSONString(lst);
    }

    /**
     * 查询我的过期优惠劵
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findExpireCoupon",method = RequestMethod.GET)
    private String findSaleExpireCoupon(HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(new Date());
        String phone = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getTelephone().toString();
        List<Coupon> lst = couponServiceImp.findSaleExpireCoupon(phone,time);
        return JSON.toJSONString(lst);
    }

    /**
     * 查询我的优惠劵
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findMyCoupon",method = RequestMethod.GET)
    private String findMyCoupon(HttpServletRequest request) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String time = sdf.format(new Date());
        String phone = userService.findByTelePhone(request.getSession().getAttribute("user").toString()).getTelephone().toString();
        List<Coupon> lst = couponServiceImp.findMyCoupon(phone,time);
        return JSON.toJSONString(lst);
    }


    /**
     * 销售员发放优惠劵，按手机号发放
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/grantCoupon",method = RequestMethod.POST)
    private String grantCoupon(HttpServletRequest request){
        Integer code = -1;
        String phone = request.getParameter("phone");
        String couponCode = request.getParameter("couponCode") ;
        Coupon coupon = couponServiceImp.findByCode(couponCode);
        if(coupon.getPhone() != null){
            code = -5000;
        }else{
            if (couponServiceImp.grantCoupon(phone,couponCode) > 0){
                code = 1;
            }
        }
        RegEntity regInfo = new RegEntity();
        regInfo.setInfoCode(code);
        regInfo.setInfoText(SysExecRsUtil.getMsgFromCode(code));
        return JSON.toJSONString(regInfo);
    }


}
